﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CraigsCrawler
{
    /// <summary>
    /// Interaction logic for EntryControl.xaml
    /// </summary>
    public partial class EntryControl : UserControl
    {
        private Entry entry;

        public EntryControl(Entry entry)
        {
            InitializeComponent();
            this.Entry = entry;
        }

        public Entry Entry
        {
            get { return entry; }
            set
            {
                entry = value;
                entry.control = this;
                TitleLabel.Text = entry.title;
                LinkLabel.Text = entry.link;
                LinkLabel.TextDecorations = TextDecorations.Underline;
                LinkLabel.Foreground = Brushes.Blue;
                InfoLabel.Text = entry.posted.ToShortDateString() + "\n" + entry.city;

                if (!string.IsNullOrEmpty(entry.thumbnail)) AddThumbnail(entry.thumbnail);

                RefreshButtons();
            }
        }

        public void AddThumbnail(string filename)
        {
            Application.Current.Dispatcher.Invoke(() =>
                {
                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.UriSource = new Uri(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CraigsCrawler", entry.parent.Name, filename), UriKind.Relative);
                    image.CacheOption = BitmapCacheOption.OnLoad;
                    image.EndInit();
                    Thumbnail.Source = image;
                    Thumbnail.Stretch = Stretch.None;
                });
        }

        public void RefreshButtons()
        {
            InterestedButton.IsEnabled = entry.entryState != EntryState.Interested;
            IgnoreButton.IsEnabled = entry.entryState != EntryState.ManuallyIgnored;
        }

        private void InterestedButton_Click(object sender, RoutedEventArgs e)
        {
            entry.entryState = (entry.entryState == EntryState.Interested) ? EntryState.New : EntryState.Interested;
            RefreshButtons();
        }

        private void SuppressButton_Click(object sender, RoutedEventArgs e)
        {
            Utilities.SuppressDuplicates(entry);
            RefreshButtons();
        }

        private void IgnoreButton_Click(object sender, RoutedEventArgs e)
        {
            entry.entryState = (entry.entryState == EntryState.ManuallyIgnored) ? EntryState.New : EntryState.ManuallyIgnored;
            Querier.Current.TailQueueByContext(entry);
            RefreshButtons();
        }

        private void LinkLabel_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            System.Diagnostics.Process.Start(entry.link);
        }
    }
}
